#include "Acceptor.hpp"
#include "EventLoop.hpp"
#include "TcpConnection.hpp"
#include <iostream>


using std::cout;
using std::endl;


void onConnection(wd::TcpConnectionPtr conn)
{
    //连接建立时的处理
    cout << conn->toString() << " has connected." << endl;

}

void onMessage(wd::TcpConnectionPtr conn)
{
    //读取数据
    string msg = conn->receive();
    cout << "receive:" << msg << endl;
    //进行业务逻辑的处理
    //decode
    //compute
    //encode
    //发送数据
    conn->send(msg);
}

void onClose(wd::TcpConnectionPtr conn)
{
    //连接断开时的处理
    cout << conn->toString() << " has closed." << endl;
}

int main()
{
    wd::Acceptor acceptor(8000);
    acceptor.ready();

    cout << "server start listening" << endl;

    wd::EventLoop loop(acceptor);
    loop.setAllCallbacks(onConnection, onMessage, onClose);

    loop.loop();

    return 0;
}

